var express = require('express');
var router = express.Router();

router.all('/*', function(req, res, next) {
    if(!req.model.results) {
        req.model.results = [];
    }
    next();
});

router.post('/*', function(req, res) {
    var sQuery = req.body.query;
    var type = req.body.type_of_search;
    req.pool.acquire(function(err, conn) {
        conn.lcall('search.search', { s_query: sQuery, type_of_search: type }, function(err, result) {
            req.pool.release(conn);
            req.model.results = result;
            req.session.model = req.model;
            res.redirect('/Search/');
        });
    });
});

router.get('/', function(req, res) {
    res.render('search', req.model);
});

module.exports = router;